package com.mldong.modules.cms.controller;

import com.mldong.auth.Perm;
import com.mldong.base.CommonPage;
import com.mldong.base.CommonResult;
import com.mldong.base.IdParam;
import com.mldong.base.IdsParam;
import com.mldong.modules.cms.dto.CategoryPageParam;
import com.mldong.modules.cms.dto.CategoryParam;
import com.mldong.modules.cms.service.CategoryService;
import com.mldong.modules.cms.vo.CategoryVO;
import com.mldong.validation.Groups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import com.mldong.modules.cms.entity.Category;
/**
* <p>
    * 栏目 前端控制器
    * </p>
*
* @author mldong
* @since 2022-10-23
*/
@RestController
@Api(tags = "栏目管理")
@RequiredArgsConstructor
public class CategoryController {
    private final CategoryService categoryService;
    /**
     * 添加栏目
     * @param param
     * @return
     */
    @PostMapping("/cms/category/save")
    @ApiOperation(value = "添加栏目")
    @Perm
    public CommonResult<?> save(@RequestBody @Validated({Groups.Save.class}) CategoryParam param) {
        categoryService.save(param);
        return CommonResult.ok();
    }
    /**
     * 删除栏目
     * @param param
     * @return
     */
    @PostMapping("/cms/category/remove")
    @ApiOperation(value = "删除栏目")
    @Perm
    public CommonResult<?> remove(@RequestBody IdsParam param) {
        categoryService.removeBatchByIds(param.getIds());
        return CommonResult.ok();
    }
    /**
     * 修改栏目
     * @param param
     * @return
     */
    @PostMapping("/cms/category/update")
    @ApiOperation(value = "修改栏目")
    @Perm
    public CommonResult<?> update(@RequestBody @Validated({Groups.Update.class}) CategoryParam param) {
        categoryService.update(param);
        return CommonResult.ok();
    }
    /**
     * 查询单个栏目
     * @param param
     * @return
     */
    @PostMapping("/cms/category/detail")
    @ApiOperation(value = "查询单个栏目")
    @Perm
    public CommonResult<CategoryVO> detail(@RequestBody IdParam param) {
        CategoryVO category = categoryService.findById(param.getId());
        return CommonResult.data(category);
    }
    /**
     *分页查询栏目列表
     * @param param
     * @return
     */
    @PostMapping("/cms/category/page")
    @ApiOperation(value = "分页查询栏目列表")
    @Perm
    public CommonResult<CommonPage<CategoryVO>> page(@RequestBody CategoryPageParam param) {
        return CommonResult.data(categoryService.page(param));
    }
    /**
     *查询栏目列表
     * @param param
     * @return
     */
    @PostMapping("/cms/category/list")
    @ApiOperation(value = "查询栏目列表")
    @Perm
    public CommonResult<List<Category>> list(@RequestBody CategoryPageParam param) {
        return CommonResult.data(categoryService.list(param.buildQueryWrapper()));
    }
}
